package com.example.chepai_test.car.controller;


import com.example.chepai_test.car.domain.Illegal;
import com.example.chepai_test.car.domain.Rg;
import com.example.chepai_test.car.sb.ChePaiShiBie;
import com.example.chepai_test.car.sb.ZLShiBie;
import com.example.chepai_test.car.service.CpService;
import org.bytedeco.opencv.presets.opencv_core;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dt")
public class DTController {

    @Autowired
    private CpService cpService;


    @GetMapping("/seephysical")
    public String seephysical(){
        return "/physicalSimulation";
    }

    @GetMapping("/seeroadCon")
    public String seeroadCon(){
        return "/roadCondition";
    }

    @GetMapping("/seemonitor")
    public String seemonitor(Model model){
        String result = ChePaiShiBie.ChePaiShiBie();
        JSONObject jsonobj = new JSONObject(result);
        JSONArray words_result = jsonobj.getJSONArray("words_result");
        System.out.println(words_result.length());
        List<String> cplist = new ArrayList<String>();
        for (int i = 0; i < words_result.length(); i++) {
            JSONObject sigcp = words_result.getJSONObject(i);
            String cp = sigcp.getString("number");
            System.out.println(sigcp.getString("number"));
            cplist.add(cp);
        }
        model.addAttribute("cplist",cplist);
        return "/roadMonitor";
    }
    @GetMapping("/seequery")
    public String seequery(Model model){
        String result = ChePaiShiBie.ChePaiShiBie();
        JSONObject jsonobj = new JSONObject(result);
        JSONArray words_result = jsonobj.getJSONArray("words_result");
        System.out.println(words_result.length());
        List<String> cplist = new ArrayList<String>();
        for (int i = 0; i < words_result.length(); i++) {
            JSONObject sigcp = words_result.getJSONObject(i);
            String cp = sigcp.getString("number");
            System.out.println(sigcp.getString("number"));
            cplist.add(cp);
        }
        model.addAttribute("cplist",cplist);
        List<Illegal> illegals = cpService.getAllIllegal();
        List<Illegal> wflist = new ArrayList<Illegal>();
        for (String s : cplist) {
            for (Illegal illegal : illegals) {
                if (s.equals(illegal.getNumber())){
                    wflist.add(illegal);
                }
            }
        }
        model.addAttribute("wflist",wflist);


        String result1= ZLShiBie.plant();
        JSONObject jsonobj1 = new JSONObject(result1);
        System.out.println(result1);
        JSONArray words_result1 = jsonobj1.getJSONArray("result");
        List<String> cplist1 = new ArrayList<String>();
            JSONObject sigcp1 = words_result1.getJSONObject(3);
            String cp = sigcp1.getString("keyword");
            System.out.println(sigcp1.getString("keyword"));
            cplist1.add(cp);
        model.addAttribute("cplist1",cplist1);



        return "/queryStatistic";
    }
    @GetMapping("/seetraffic")
    public String seetraffic(){
        return "/trafficController";
    }

    @PostMapping("/rg_table")
    public String rg_table(Rg rg){
        System.out.println(rg);
        return "/trafficController";
    }




}
